pipeline {
    agent any
    // agent {
    //     kubernetes {
    //         defaultContainer 'nodejs'
    //         yamlFile 'podTemplates/nodejs-16.13.yaml'
    //     }
    // }
    stages {
        stage('Preparation') {
            steps {
                git url: 'https://gitee.com/zheng11581/RuoYi-Vue.git' 
                // container('git') {
                //     git url: 'https://gitee.com/zheng11581/RuoYi-Vue.git'       
                // }                
            }
        }

        stage('Build') {
            steps{
                // 使用动态agent{ kubernetes{} }节点：defaultContainer
                // sh '''
                //     cd ruoyi-ui
                //     npm install --registry=https://registry.npm.taobao.org
                //     npm run build:prod
                // '''

                // 使用全局工具：NodeJs16.13，定义全局工具时别名不要包含空格
                nodejs(nodeJSInstallationName: 'NodeJs16.13') {
                    sh '''
                        cd ruoyi-ui
                        npm install --registry=https://registry.npm.taobao.org
                        npm run build:prod
                        cd dist
                        zip dist.zip *
                    '''
                }
            }
        }
        stage('Deploy') {
            steps {
                echo "Deploy"
                // 此步骤需要安装Publish Over SSH插件，https://plugins.jenkins.io/publish-over-ssh/
                // 配置Publish over SSH
                sshPublisher(
                    publishers: [
                        sshPublisherDesc( 
                            configName: 'ruoyi_test', 
                            transfers: [
                                sshTransfer(
                                    execCommand: 'unzip dist.zip',
                                    remoteDirectory: '/var/www/html', 
                                    sourceFiles: 'dist/dist.zip',
                                    // 下面是默认配置
                                    cleanRemote: false, excludes: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false, removePrefix: '')
                            ], 
                            usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false
                        )
                    ]
                )
            }
        }
    }
    post { 
        always { 
          echo 'always'
        } 
    }
}